package com.fxm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxm.dao.RuleMapper;
import com.fxm.domain.Rule;
import com.fxm.domain.User;
import com.fxm.returns.RespCode;
import com.fxm.returns.RespEntity;
import com.fxm.returns.RespTable;
import com.fxm.service.RuleService;
import com.fxm.service.UserService;
import com.fxm.vo.param.RuleParam;
import com.fxm.vo.result.RuleResult;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 答题规则表 服务实现类
 * </p>
 *
 * @author fxm
 * @since 2021-02-22
 */
@Slf4j
@Service
public class RuleServiceImpl extends ServiceImpl<RuleMapper, Rule> implements RuleService {

    @Autowired
    private UserService userService;

    @Override
    public RespTable<List<RuleResult>> ruleList(Integer page, Integer limit, String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getDeleted, 0);
        if (StringUtils.isNotBlank(username)) {
            queryWrapper.lambda().like(User::getUsername, username);
        }
        IPage<User> iPage = new Page<>(page, limit);
        IPage<User> pg = userService.page(iPage, queryWrapper);
        List<User> userList = pg.getRecords();
        if (Objects.isNull(userList) || userList.size() == 0) {
            return new RespTable(RespCode.OK, null, 0);
        }
        //数据组装
        List<RuleResult> ruleResultList = dataPackage(userList);
        return new RespTable(RespCode.OK, ruleResultList, pg.getTotal());
    }

    /**
     * 数据组装
     * @param userList
     * @return
     */
    private List<RuleResult> dataPackage(List<User> userList) {
        List<RuleResult> ruleResultList = Lists.newArrayList();

        for (User user : userList) {
            QueryWrapper<Rule> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Rule::getOpenid, user.getOpenid())
                                 .eq(Rule::getDeleted, 0);
            Rule rule = baseMapper.selectOne(queryWrapper);

            RuleResult ruleResult = RuleResult.builder()
                    .id(rule.getId())
                    .openid(rule.getOpenid())
                    .username(user.getUsername())
                    .time(rule.getRule())
                    .challenge(rule.getChallenge())
                    .build();

            ruleResultList.add(ruleResult);
        }

        return ruleResultList;
    }

    @Override
    public RespEntity updateRule(RuleParam ruleParam) {
        Rule rule = baseMapper.selectById(ruleParam.getId());
        if (Objects.nonNull(ruleParam.getTime()) && ruleParam.getTime() != 0) {
            rule.setRule(ruleParam.getTime());
        }
        if (Objects.nonNull(ruleParam.getChallenge())) {
            rule.setChallenge(ruleParam.getChallenge());
        }
        baseMapper.updateById(rule);
        return new RespEntity(RespCode.SUCCESS);
    }
}
